<template>
  <view class="loginbg">
    <van-toast id="van-toast" />
    <view class="tips">为提供更好的使用体验，当前页面需要授权微信登录</view>
    <view class="denglu2"><button @tap="getuserinfo">点击授权登录</button></view>
  </view>
</template>


<script>
import Taro from '@tarojs/taro';
import { Current } from '@tarojs/taro';
import { accredit } from '@api/login';
import Toast from '@components/vant/toast/toast';
export default {
  data() {
    return {
      sq: '',
    };
  },
  mounted() {
    const { sq } = Current.router.params;
    this.sq = sq;
  },
  methods: {
    getuserinfo() {
      const that = this;
      // const {sq}=Current.router.params
      // console.log(sq,'这是sq');
      var gsid = Taro.getStorageSync('gsid');
       
      Taro.login({
        success: function (res) {
          if (res.code) {
            Taro.request({
              url: 'https://xxyg.daxinxin.vip/api.php/home/index/getwxuser',
              data: {
                code: res.code,
              },
              success: function (e) {
                if (e.data.code == 0) {
                  //_that.loadding(false);
                  Toast(e.data.data.msg);
                  Taro.hideLoading();
                } else {
                  Taro.setStorageSync('session_id', e.data.data.openid);
                  const session_id = e.data.data.openid;
                  // An highlighted block
                  wx.showModal({
                    title: '温馨提示',
                    content: '正在请求您的个人信息',
                    success(res) {
                      if (res.confirm) {
                        Taro.showLoading({
                          title: '授权中',
                        });
                        wx.getUserProfile({
                          desc: '获取你的昵称、头像、地区及性别',
                          success: (res) => {
                            console.log(res);
                            const { nickName, avatarUrl } = res.userInfo;
                            accredit({
                              session_id: session_id,
                              nick_name: nickName,
                              xcxavatarurl: avatarUrl,
                              gsid: gsid,
                            }).then((res) => {
                              Taro.hideLoading();
                              if (!res.code) {
                                if (that.sq == 1) {
                                  const { id, jtid } = Current.router.params;
                                  Toast({
                                    type: 'success',
                                    message: '授权成功',
                                    duration: 500,
                                    onClose: () => {
                                      Taro.reLaunch({
                                        url: `/pages/entry/index?id=${id}&jtid=${jtid}`,
                                      });
                                    },
                                  });
                                } else {
                                  Toast({
                                    type: 'success',
                                    message: '授权成功',
                                    duration: 500,
                                    onClose: () => {
                                      Taro.reLaunch({
                                        url: '/pages/index/index',
                                      });
                                    },
                                  });
                                }
                              }
                            });
                          },
                          fail: (res) => {
                            //拒绝授权
                            Taro.hideLoading();
                            that.showErrorModal('您拒绝了请求');
                            return;
                          },
                        });
                      } else if (res.cancel) {
                        //拒绝授权 showErrorModal是自定义的提示
                        that.showErrorModal('您拒绝了请求');
                        return;
                      }
                    },
                  });

                  // Taro.getSetting({
                  //   success(res) {
                  //     if (res.authSetting['scope.userInfo']) {
                  //       // 已经授权，可以直接调用 getUserInfo 获取头像昵称
                  //       Taro.getUserInfo({
                  //         success: function (res) {
                  //           const { nickName, avatarUrl } = res.userInfo;
                  //           accredit({
                  //             session_id: session_id,
                  //             nick_name: nickName,
                  //             xcxavatarurl: avatarUrl,
                  //             gsid: gsid,
                  //           }).then((res) => {
                  //             Taro.hideLoading();
                  //             if (!res.code) {
                  //               if (that.sq == 1) {
                  //                 const { id, jtid } = Current.router.params;
                  //                 Toast({
                  //                   type: 'success',
                  //                   message: '授权成功',
                  //                   duration: 500,
                  //                   onClose: () => {
                  //                     Taro.reLaunch({
                  //                       url: `/pages/entry/index?id=${id}&jtid=${jtid}`,
                  //                     });
                  //                   },
                  //                 });
                  //               } else {
                  //                 Toast({
                  //                   type: 'success',
                  //                   message: '授权成功',
                  //                   duration: 500,
                  //                   onClose: () => {
                  //                     Taro.reLaunch({
                  //                       url: '/pages/index/index',
                  //                     });
                  //                   },
                  //                 });
                  //               }
                  //             }
                  //           });
                  //         },
                  //       });
                  //     }
                  //   },
                  // });
                }
              },
            });
          } else {
            Toast.fail('授权失败,返回首页');
            Taro.reLaunch({
              url: '/pages/index/index',
            });
          }
        },
      });
    },
  },
};
</script>

<style>
/* pages/index/login.wxss */

page {
  width: 100%;
  height: 100%;
  background-color: #f7f7f7;
}

.loginbg {
  width: 100%;
  height: 100vh;
  background-color: #f4f6f7;
  background-image: url('https://xxyg.daxinxin.vip/wxapp/loginbg.png');
  background-position: bottom center;
  background-size: 100%;
  background-repeat: no-repeat;
}
.denglu2 {
  width: 60%;
  justify-content: center;
  align-items: center;
  height: 60rpx;
  line-height: 60rpx;
  margin: 0 auto;
}
.denglu2 button {
  justify-content: center;
  align-items: center;
  height: 70rpx;
  line-height: 70rpx;
  margin: 0 auto;
  background-color: #46bdf6;
  color: #fff;
  border-radius: 35rpx;
  display: block;
}
.tips {
  color: rgb(182, 176, 176);
  width: 100%;
  text-align: center;
  font-size: 30rpx;
  height: 50rpx;
  line-height: 50rpx;
  padding-top: 66%;
  margin-bottom: 50rpx;
}
</style>